package com.lanou.web.controller;

import com.lanou.entity.TbItem;
import com.lanou.entity.TbItemDesc;
import com.lanou.entity.TbItemParamItem;
import com.lanou.service.ItemService;
import com.lanou.utils.ShiHuiResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by QuChao on 2018/11/19
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @ApiOperation("将数据库数据存进缓存")
    @PutMapping("/cache")
    public void addCache() {
        itemService.addCache();
    }

    @ApiOperation("获取数据，结果为三个对象")
    @GetMapping("/cache/{id}")
    public Map<String, Object> getCache(@PathVariable("id") Long id) {
        Map<String, Object> infoById = itemService.getCacheById(id);
        return infoById != null ? ShiHuiResult.ok(infoById) : ShiHuiResult.error(400);
    }

    @ApiOperation("获取Item对象")
    @GetMapping("/{id}")
    public Map<String, Object> getItemInfo(@PathVariable("id") Long id) {
        TbItem tbItem = itemService.getItemInfoById(id);
        return tbItem != null ? ShiHuiResult.ok(tbItem) : ShiHuiResult.error(400);
    }

    @ApiOperation("获取ItemDesc对象")
    @GetMapping("/desc/{id}")
    public Map<String, Object> getItemDescInfo(@PathVariable("id") Long id) {
        TbItemDesc itemDesc = itemService.getItemDescInfoById(id);
        return itemDesc != null ? ShiHuiResult.ok(itemDesc) : ShiHuiResult.error(400);
    }

    @ApiOperation("获取ItemParamItem对象")
    @GetMapping("/param/{id}")
    public Map<String, Object> getItemParamItemInfo(@PathVariable("id") Long id) {
        TbItemParamItem tbItemParamItem = itemService.getItemParamItemInfoById(id);
        return tbItemParamItem != null ? ShiHuiResult.ok(tbItemParamItem) : ShiHuiResult.error(400);
    }

    @ApiOperation("更新缓存数据")
    @PutMapping("/cache/{id}")
    public Map<String, Object> updataById(@PathVariable("id") Long id) {
        boolean result = itemService.updataById(id);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }

    @ApiOperation("删除缓存中指定的数据")
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Long id) {
        boolean result = itemService.deleteById(id);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }
}
